TARGET = app
OBJDIR = build

CROSS_COMPILE := 

CC=$(CROSS_COMPILE)gcc
CXX=$(CROSS_COMPILE)g++
LD=$(CROSS_COMPILE)g++
STRIP=$(CROSS_COMPILE)strip

INCLUDES += -I./utils
INCLUDES += -I./config

MAIN_OBJS += main.o dispatch.o 
MAIN_OBJS += config/uconfig.o config/cJSON.o
MAIN_OBJS += utils/lock.o utils/log.o utils/wait.o 

CFLAGS += $(MODULES)
CFLAGS += -g -Wall -O3
CXXFLAGS += $(MODULES)
CXXFLAGS += -g -O3 -std=c++11

LDFLAGS += -g  -O3
LDFLAGS += $(LIBS)
LDFLAGS += -lpthread -lm

VPATH += utils
VPATH += config
OBJS = $(patsubst %, $(OBJDIR)/%, $(notdir $(MAIN_OBJS)))
                                                                                                                        
all:dir_create $(TARGET)

$(TARGET):$(OBJS)
	$(LD) $(OBJS) -o $(TARGET)_g $(LDFLAGS) 
	$(STRIP) --strip-unneeded -o $(TARGET) $(TARGET)_g

define CREATE_DIR
	if [ ! -d $(1) ];\
	 	then\
    	mkdir -p $(1);\
	fi	
endef

dir_create:  
	@$(call CREATE_DIR,$(OBJDIR))

clean:
	rm -rf $(OBJDIR)/*
	rm -f $(TARGET)_g $(TARGET)

$(OBJDIR)/%.o:%.S
	@echo "CC $<"
	@$(CC) $(INCLUDES) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.o:%.c
	@echo "CC $<"
	@$(CC) $(INCLUDES) $(CFLAGS) -o $@ -c $< 
$(OBJDIR)/%.o:%.cpp
	@echo "CXX $<"
	@$(CXX) $(INCLUDES) $(CXXFLAGS) -o $@ -c $< 


